package com.howinfun.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.howinfun.common.Result;
import com.howinfun.netty.pool.ClientChannelPool;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Howinfun
 * @desc
 * @date 2019/10/21
 */
@RestController
@RequestMapping("/wsController")
@Slf4j
public class WebSocketController {

    @RequestMapping("/sendToUser/{clientId}")
    public Result sendToUser(@PathVariable String clientId) {
        ClientChannelPool.getChannel(clientId).writeAndFlush(new TextWebSocketFrame("嘻嘻"));
        return Result.ok("发送成功");
    }
    @RequestMapping("/getQrCode/{clientId}")
    public Result getQrCode(@PathVariable String clientId,String mark,String money,String keyId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mark",mark);
        jsonObject.put("money",money);
        jsonObject.put("key_id",keyId);
        jsonObject.put("messageuuid","mid" + mark);
        jsonObject.put("paytype","alipay");
        jsonObject.put("type","paytype");
        log.info("clientId:{} msg:{}",clientId,JSON.toJSONString(jsonObject));
        ClientChannelPool.getChannel(clientId).writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(jsonObject)));
        return Result.ok("发送成功");

    }
}
